package com.example.fbi_text.Acitivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SwitchCompat;

import com.example.fbi_text.R;
import com.example.fbi_text.utils.SpUtils;
import com.example.fbi_text.views.AccountFragment;
import com.example.fbi_text.views.Myselffragment;
//设置功能
public class SettingsActivity extends AppCompatActivity implements View.OnClickListener {

    private SwitchCompat mAdminSwitch;
    private RelativeLayout add_text_view;
    private RelativeLayout add_goods_view;
    private ImageView add_settings_back;
    private RelativeLayout account_management;
    private Button logout_button;
    private SpUtils mSpUtils;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);


        add_text_view=findViewById(R.id.add_text_view);
        add_goods_view=findViewById(R.id.add_goods_view);
        mAdminSwitch=findViewById(R.id.admin_switch);
        add_settings_back=findViewById(R.id.add_settings_back);
        account_management=findViewById(R.id.account_management);
        logout_button=findViewById(R.id.logout_button);
        mSpUtils = new SpUtils(this);


        add_text_view.setOnClickListener(this);
        add_goods_view.setOnClickListener(this);
        add_settings_back.setOnClickListener(this);
        account_management.setOnClickListener(this);
        logout_button.setOnClickListener(this);
        mAdminSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                //开关监听，判断是否显示"添加文章"和"添加商品"
                showAdminViews(isChecked);
            }
        });
    }



    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.add_text_view){ //跳转添加文章界面
            Intent intent = new Intent(SettingsActivity.this, AddTextActivity.class);
            startActivity(intent);
        } else if (id == R.id.add_goods_view) {//跳转添加商品界面
            Intent intent = new Intent(SettingsActivity.this, AddGoodsActivity.class);
            startActivity(intent);
        } else if (id == R.id.add_settings_back) {//返回按钮
            finish();
        } else if (id == R.id.account_management) {//跳转账号管理界面
            Intent intent = new Intent(SettingsActivity.this, AccountActivity.class);
            startActivity(intent);
        } else if (id == R.id.logout_button) {//退出登录
            mSpUtils.putInt(SpUtils.LOGIN_KEY, -1);
            Intent intent = new Intent(SettingsActivity.this, LoginActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
        }
    }

    private void showAdminViews(boolean show) {
        View view = findViewById(R.id.admin_views);
        view.setVisibility(show ? View.VISIBLE : View.GONE);
    }
}
